package com.cloud.rou.im.core.process;


import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * <p></p>
 *
 * @author GaoShuangWen
 * @since 2019-11-09 21:21
 */
@Slf4j
public abstract class AbstractClientIMProcessor<K, T> implements IMProcessor<T> {

    private Map<K, Consumer<T>> maps;

    public AbstractClientIMProcessor() {
        this.maps = new HashMap<>();
        registryProcessor();
    }

    @Override
    public void process(ChannelHandlerContext ctx, T t) {
        Consumer<T> consumer = maps.get(getMapKey(t));
        if (consumer == null) {
            log.warn("[message processor] unexpected msg: {}", t.toString());
        } else {
            consumer.accept(t);
        }
    }

    protected abstract K getMapKey(T t);

    public void registry(K k, Consumer<T> consumer) {
        maps.put(k, consumer);
    }

    /**
     * registry processor
     */
    public abstract void registryProcessor();

}
